package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"userprofile/models/mysql"

	"userprofile/userprofilerpc/internal/svc"
	"userprofile/userprofilerpc/userprofilerpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *userprofilerpc.UpdateRequest) (*userprofilerpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line

	update := mysql.Account{
		Model:     gorm.Model{ID: uint(in.Id)},
		Img:       in.Img,
		Name:      in.Name,
		Codes:     in.Codes,
		Sex:       in.Sex,
		Introduce: in.Introduce,
	}

	err := mysql.DB.Debug().Model(&update).Updates(&update).Error

	if err != nil {
		return nil, fmt.Errorf("修改失败")
	}

	return &userprofilerpc.UpdateResponse{Id: int64(update.ID)}, nil
}
